<?php
/*
 * @Author: sunkaiyuan sunkaiyuan@namenode.com
 * @Date: 2022-12-08 12:12:29
 * @LastEditors: 孙开源 && sunkaiyuan@namenode.cn
 * @LastEditTime: 2023-08-28 13:13:36
 * @Description:
 */

namespace App\Listeners;

use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Support\Facades\Log;

use Exception;


class SubscribeListener implements ShouldQueue
    {
    use InteractsWithQueue;


    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
        {
        }

    public function shouldQueue()
        {
        return true;
        }
    public function handle($event)
        {
        $event->router?->subscribe->filter(function ($row) use ($event)
            {
            $data['token'] = $event->token;
            $data['data']  = $event->data;
            \App\Services\HttpService::post(
                $row->url,
                $data,
                $row->token,
                function ($data)
                    {
                    Log::info(json_encode($data->getBody()->getContents()));
                    }
                ,
                function (Exception $e) use ($event, $row)
                    {
                    Log::info("接口:{$row->remark}错误！" . $e->getMessage());
                    }
            );

            });
        }
    }